package org.devknights.android.moneyeye.core.activities;

import java.util.ArrayList;
import java.util.List;

import org.devknights.android.R;
import org.devknights.android.moneyeye.MoneyEye;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class AccountDetails extends Activity 
{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.details);
		
		Intent intent = getIntent();     
		
		String message = intent.getStringExtra(MoneyEye.DATA); 
		
		TableLayout tableLayout = (TableLayout)findViewById(R.id.table);	
		
		String rows[] = {"Account Name="+message,"Account No=XXXXXXXXXXXX6541","Account Type=Credit","Spends=10000.00","Account Limit=25000.00"};
		
		final int [] rowIds = new int[]{R.id.tableRow1,R.id.tableRow2,R.id.tableRow3,R.id.tableRow4,R.id.tableRow5};
		
		View.OnClickListener btnListenerArr [] = new View.OnClickListener[5];
		
		btnListenerArr[0] = new View.OnClickListener() {
			
			@Override
			public void onClick(View v) 
			{
				
				TableRow row = (TableRow) findViewById(R.id.tableRow1);
				
				Button btn = (Button)row.getChildAt(2);
				
				
				if("EDIT".equals(btn.getTag()))
				{
				
				TextView label = (TextView)row.getChildAt(1);
				
				EditText editText = new EditText(getApplicationContext());				
				
				editText.setText(label.getText());	
				editText.setOnFocusChangeListener(new OnFocusChangeListener(){
					
					@Override
					public void onFocusChange(View v, boolean hasFocus) 
					{
						if(!hasFocus)
						{
							
						}							
					}
				});				
				
				btn.setBackgroundResource(android.R.drawable.ic_menu_save);
				btn.setTag("SAVE");
				
				row.removeViewAt(1);
				row.addView(editText, 1);	
				
				}
				else
				{
					EditText label = (EditText)row.getChildAt(1);
					
					TextView textLabel = new TextView(getApplicationContext());							
					textLabel.setText(label.getText());				
										
					btn.setBackgroundResource(android.R.drawable.ic_menu_edit);
					btn.setTag("EDIT");
					
					row.removeViewAt(1);
					row.addView(textLabel, 1);	
					
					
					CharSequence text = "Saved Successfully";
					int duration = Toast.LENGTH_SHORT;
					Toast toast = Toast.makeText(getApplicationContext(), text, duration);
					toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_VERTICAL, 0, 0);
					toast.show();
				}
			}
		};
		
		btnListenerArr[2] = new View.OnClickListener() {
			
			@Override
			public void onClick(View v) 
			{
							
				TableRow row = (TableRow) findViewById(R.id.tableRow3);			
				
				// Spinner element
		        Spinner spinner = new Spinner(getApplicationContext());
		 
		        // Spinner click listener
		        //spinner.setOnItemSelectedListener(this);
		 
		        // Spinner Drop down elements
		        List<String> categories = new ArrayList<String>();
		        categories.add("Credit");
		        categories.add("Debit");		        
		 
		        // Creating adapter for spinner
		        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.spinner_row , categories);
		 
		        // Drop down layout style - list view with radio button
		        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		 
		        // attaching data adapter to spinner
		        spinner.setAdapter(dataAdapter);		        
				
				row.removeViewAt(1);
				row.addView(spinner, 1);					
			}
		};
		
		btnListenerArr[4] = new View.OnClickListener() {
			
			@Override
			public void onClick(View v) 
			{				
				
				TableRow row = (TableRow) findViewById(R.id.tableRow5);
				
				TextView label = (TextView)row.getChildAt(1);				
				
				EditText editBox = new EditText(getApplicationContext());
				editBox.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
				editBox.setText(label.getText());
					
				editBox.setOnFocusChangeListener(new OnFocusChangeListener(){
					
					@Override
					public void onFocusChange(View v, boolean hasFocus) 
					{
						if(!hasFocus)
						{
							
						}							
					}
				});
				
				row.removeViewAt(1);
				row.addView(editBox, 1);					
			}
		};
		
		for (int i = 0; i < rows.length; i++) 
		{		
			TableRow tableRow = (TableRow)findViewById(rowIds[i]);
			
			tableRow.setLayoutParams(new TableRow.LayoutParams(
			         TableRow.LayoutParams.MATCH_PARENT,
			         TableRow.LayoutParams.WRAP_CONTENT));
			
			final String[] cols = rows[i].split("=");

			for (int j = 0; j < cols.length; j++) 
			{
					TextView columsView = new TextView(getApplicationContext());					
					columsView.setText(String.format("%7s", cols[j]));
					columsView.setPadding(10,10,10,10);				
					tableRow.addView(columsView);				
			}
			
			if((i+1) % 2 != 0)
			{
			
			Button button = new Button(getApplicationContext());			
			//button.setLayoutParams(new TableRow.LayoutParams(40,40));
			TableRow.LayoutParams params = new TableRow.LayoutParams(40,40);
			params.gravity = Gravity.CENTER_VERTICAL;
			params.leftMargin = 150;
			
			button.setLayoutParams(params);
			
			button.setBackgroundResource(android.R.drawable.ic_menu_edit);
			button.setTag("EDIT");
			button.setOnClickListener(btnListenerArr[i]);
			
			tableRow.addView(button);
			
			}			
		}
		
		
		
		List<String> smsList = new ArrayList<String>();
		
		smsList.add("Thank you for using your HDFC bank CREDIT card ending 6541 for Rs.24990.00 in GURGAON at CRAZEAL9430 on 2013-07-04:19:31:43.");
		smsList.add("Thank you for using your HDFC bank CREDIT card ending 6541 for Rs.10990.00 in BANGALORE at CRAZEAL9430 on 2013-07-04:19:31:43.");

		//ListView smsListView = (ListView) findViewById(R.id.SMSList);
		ListView smsListView = new ListView(getApplicationContext());
		smsListView.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, smsList));
		tableLayout.addView(smsListView);
	}
	

}
